Ext.namespace("GridUnitInfo");
GridUnitInfo.Detail = function() {
	
	this.txtSchoolId = new Ext.form.Hidden({
		name:'SchoolId'
	});
	//营销单元id
	this.txtUnitId = new Ext.form.Hidden({
		name:'UnitId',
		value:''
	});
	this.txtGridId = new Ext.form.Hidden({
		name:'GridId'
	});
	//学校名称
	this.txtUnitName = new Ext.form.TextField({
		fieldLabel : '营销单元名称<font color="red">*</font>',
		name :'UnitName',
		width : 300,
		maxLength : 100,
		allowBlank:false
	});
	//营销单元类型
	this.unitType = new Ext.form.ComboBox({
        width: 120,
        fieldLabel: '营销单元类型<font color="red">*</font>',
        store: OMS.GRID_UNIT_TYPE,
        typeAhead:true,
        mode: 'local',
        editable: false,
        triggerAction: 'all',
        emptyText:'---请选择---',
        selectOnFocus: true
    });
	//主要联系人
	this.txtNearlyHallName = new Ext.form.TextField({
		fieldLabel : '最近的营业厅名称',
		name :'occupancyRate',
		width : 300,
		maxLength : 25
	});
	
	//街道路段
	this.txtAddressInfoNeighbourhood = new Ext.form.TextField({
		fieldLabel : '街道路段',
		name : 'neighbourhood',
		width : 300,
		maxLength : 10
	});
	this.txtAddressInfoStreet = new Ext.form.TextField({
		fieldLabel : '地址路',
		name :'street',
		width : 300,
		maxLength : 10
	});
	//弄
	this.txtAddressInfoAlley = new Ext.form.NumberField({
		fieldLabel: '弄',
		name :'alley',
		width: 300,
		maxLength : 10
	});
	//号
	this.txtAddressInfoDoorNum = new Ext.form.NumberField({
		fieldLabel: '号',
		name :'doorNum',
		width: 300,
		maxLength : 10
	});
	//基本信息
	this.form = new Ext.form.FormPanel({
		bodyStyle : 'padding: 5px 5px 0',
		frame :true,
		//fileUpload: true, 
		autoScroll : true,
		//autoHeight : true,
		height: 390,
		width : 750,
		labelAlign: 'right',
		labelWidth : 120,
		layout : 'table',
		layoutConfig : {columns:2},
		items:[
			{colspan:2,layout:'form',items:[this.txtUnitName]},
			{colspan:2,layout:'form',items:[this.unitType]},			
            {colspan:2,layout:'form', items:[this.txtNearlyHallName]}, 
            {colspan:2,layout:'form',items:[this.txtAddressInfoNeighbourhood]},	
            {colspan:2,layout:'form', items:[this.txtAddressInfoStreet]}, 
            {colspan:2,layout:'form', items:[this.txtAddressInfoAlley]},
            {colspan:2,layout:'form', items:[this.txtAddressInfoDoorNum]},
			{colspan:2,layout:'form', items:[this.txtGridId]}
		]
	});
	
	//GIS信息
	this.txtGisId = new Ext.form.Hidden({
		name: 'gisInfo.id'
	});
	
	this.txtGisType = new Ext.form.Hidden({
		name: 'gisInfo.type',
		value: 1
	});
	 //坐标
    this.txtCoordsInfo = new Ext.form.TextArea({
		fieldLabel : '营销单元坐标',
		preventScrollbars : true,
		name :'coordsInfo',
		width : 400,
		heigth : 350
    });
    this.txtGroupCustInfoID = new Ext.form.Hidden({
    	name:'marketIdInfo'
    });
    
    //边框颜色
    this.txtLineColor = new Ext.form.ComboBox({
    	width: 100,
        fieldLabel: '边框颜色',
        name : 'LineColor',
        store: new Ext.data.SimpleStore({
        	fields: ['value', 'name'],
        	data: OMS.GIS_COLOR
        }),
        valueField: 'value',
        editable: false,
        displayField: 'name',
        typeAhead:true,
        mode: 'local',
        emptyText:'---请选择---',
        triggerAction: 'all',
        selectOnFocus: true
    });
    
    //填充颜色
    this.txtMainFillColor = new Ext.form.ComboBox({
    	width: 100,
        fieldLabel: '填充颜色',
        editable: false,
        name : 'MainFillColor',
        store: new Ext.data.SimpleStore({
        	fields: ['value', 'name'],
        	data: OMS.GIS_COLOR
        }),
        valueField: 'value',
        displayField: 'name',
        typeAhead:true,
        mode: 'local',
        emptyText:'---请选择---',
        triggerAction: 'all',
        selectOnFocus: true
    });
    //绘制学校
    this.btnDrawGis = new Ext.Button({
        text: '绘制GIS',
        tooltip: {text: '绘制GIS'},
        handler: this.viewGis,
        scope: this
    });
    
    //GIS信息--FormPanel
    this.gisform = new Ext.form.FormPanel({
		bodyStyle : 'padding: 5px 5px 0',
		frame :true,
		//autoHeight : true,
		height:420,
		id:'gisForm',
		disabled:true,
		width : 700,
		labelAlign: 'right',
		labelWidth : 120,
		layout : 'table',
		layoutConfig : {columns:2},
		items:[
			{layout:'form', items:[this.txtLineColor]},
			{layout:'form', items:[this.txtMainFillColor]},
			{colspan:2,layout:'form', items:[this.txtCoordsInfo]},
			{layout:'form', items:[this.txtGisType]},
            {colspan:2,layout:'form', items:[this.btnDrawGis]},
            {layout:'form', items:[this.txtGisId]}
		]
	});
    
	 this.tabs = new Ext.TabPanel({
        width:700,
        activeTab: 0,
        frame:true,
        //autoHeight:true,
        height : 420,
        deferredRender:false, 
		layoutOnTabChange:true,
        items:[
            {items:this.form, title: '基本信息'},
            {items:this.gisform, title: 'Gis信息'}
        ]
    });
    
	GridUnitInfo.Detail.superclass.constructor.call(this,{
		title : '营销单元信息',
		bodyStyle: 'padding: 5px 5px 0',
        autoHeight: true,
        width: 750,
        resizable: false,
        plain: true,
        modal: true,
        layout:'form',
        autoScroll: true,
        closeAction: 'hide',
        items :[this.tabs],
        buttons: [{
            text: '保存',
            cls: 'x-btn-text-icon btn-save',
            handler: this.onSave,
            scope: this
        }, {
            text: '关闭',
            cls: 'x-btn-text-icon btn-cancel',
            handler: this.hide.createDelegate(this, [])
        }]
		
	});
};

Ext.extend(GridUnitInfo.Detail,Ext.Window,{
		initAdd :function(){
			this.form.getForm().reset();
			this.gisform.getForm().reset();
		},
		initEdit : function(data){
			this.form.getForm().reset();
			this.gisform.getForm().reset();
			Ext.Ajax.request({
				url: '../GridUnitInfo/prepare.form?timesnap=' + Date.parse(new Date()),
				method: 'POST',
				scope:this,
				params: {
					school_id : data.SCHOOL_ID,
					unit_manager : data.UNIT_MANAGER,
					gis_id : data.GIS_ID,
					address_id : data.ADDRESS_ID,
					unit_id : data.UNIT_ID,
					contact_id : data.MAIN_CONTACT
					//entity_id : data.ENTITY_ID
					//entity_type :data.ENTITY_TYPE,
					//contact_type : data.CONTACT_TYPE
				},
				success:function(resp){	
					var resp = Ext.util.JSON.decode(resp.responseText);
					//基本信息
					if(resp.school){
						this.txtSchoolId.setValue(resp.school[0].SCHOOL_ID);
						this.txtUnitId.setValue(resp.school[0].UNIT_ID);			
						
						this.txtSchoolName.setValue(resp.school[0].SCHOOL_NAME);
						this.cmbSchoolType.setValue(resp.school[0].SCHOOL_TYPE);
						this.cmbSchoolChar.setValue(resp.school[0].SCHOOL_CHAR);
						this.txtTeachersNum.setValue(resp.school[0].TEACHERS_NUM);
						this.txtStudentsNum.setValue(resp.school[0].STUDENTS_NUM);
					}
					if(resp.main_contact[0]){
						this.txtMainContactId.setValue(resp.main_contact[0].CONTACT_ID);
						this.txtMainContactName.setValue(resp.main_contact[0].USER_NAME);
						this.txtMainContactPhone.setValue(resp.main_contact[0].PHONE_NUM_OFFICE);
					}		
					if(resp.unit_manager[0]){
						this.txtUnitManagerUserId.setValue(resp.unit_manager[0].USER_ID);
						this.txtUnitManager.setValue(resp.unit_manager[0].USER_NAME);
						this.txtUnitManagerPhone.setValue(resp.unit_manager[0].USER_PHONE);
					}	
					if(resp.unit){	
						this.txtGridId.setValue(resp.unit[0].GRID_ID);
						this.txtAddressId.setValue(resp.unit[0].ADDRESS_ID);
					}		
					if(resp.address[0]){
						this.txtAddressInfoStreet.setValue(resp.address[0].STREET);
						this.txtAddressInfoAlley.setValue(resp.address[0].ALLEY);
						this.txtAddressInfoDoorNum.setValue(resp.address[0].DOOR_NUM);
						this.cmbProvince.setValue(resp.address[0].DISTRICT);
						this.txtAddressInfoNeighbourhood.setValue(resp.address[0].NEIGHBOURHOOD);
					}		
					//gis信息
					/*this.cmbDistrict.setValue(resp.school[0].GRID);
					this.txtAddressInfoLongitude.setValue(resp.address[0].LONGITUDE);
					this.txtAddressInfoLatitude.setValue(resp.address[0].LATITUDE);
					this.txtGisIconId.setValue(resp.gis[0].ICON_ID);*/
					if(resp.gis[0]){
						this.txtGisId.setValue(resp.gis[0].GIS_ID);
						this.txtCoordsInfo.setValue(resp.gis[0].COORDS_INFO);
						this.txtLineColor.setValue(resp.gis[0].LINE_COLOR);
						this.txtMainFillColor.setValue(resp.gis[0].MAIN_FILL_COLOR);
						this.txtGisType.setValue(resp.gis[0].TYPE);
					}
				},
				failure:function(resp){	
					Utils.info('操作失败!','系统提示');
					return;
				}
			});
		},
		onSave:function(){
			if(!this.form.getForm().isValid()){
				Utils.info("信息填写不完整或有误,请检查!","系统提示");
				return;
			}
			this.conn =new Ext.data.Connection({
		        method: 'POST',
		        url: '../GridUnitInfo/save.form?timesnap=' + Date.parse(new Date())
		    });
			Utils.request(this.conn,{
				scope: this,
				params: {
					//基本信息
					school_id:this.txtSchoolId.getValue(),
					unit_id :this.txtUnitId.getValue(),
					grid_id : this.txtGridId.getValue(),
					school_name :this.txtSchoolName.getValue(),
					school_type :this.cmbSchoolType.getValue(),
					teachers_num :this.txtTeachersNum.getValue(),
					students_num :this.txtStudentsNum.getValue(),
					main_contact :this.txtMainContactId.getValue(),
					school_char :this.cmbSchoolChar.getValue(),
					
					contact_id:this.txtMainContactId.getValue(),
					contact_name:this.txtMainContactName.getValue(),
					contact_phone:this.txtMainContactPhone.getValue(),
						
					unit_manager:this.txtUnitManagerUserId.getValue(),	
					unit_manager_user_id:this.txtUnitManagerUserId.getValue(),
					unit_manager_user_name:this.txtUnitManager.getValue(),
					unit_manager_user_phone:this.txtUnitManagerPhone.getValue(),
					
					address_id:this.txtAddressId.getValue(),
					
					street:this.txtAddressInfoStreet.getValue(),
					alley:this.txtAddressInfoAlley.getValue(),
					door_num:this.txtAddressInfoDoorNum.getValue(),
					district:this.cmbProvince.getValue(),
					neighbourhood:this.txtAddressInfoNeighbourhood.getValue(),
							
					//gis信息
					/*grid_id:this.cmbDistrict.getValue(),
					longitude:this.txtAddressInfoLongitude.getValue(),
					latitude:this.txtAddressInfoLatitude.getValue(),
					icon_id:this.txtGisIconId.getValue(),
					gis_id:this.txtGisId.getValue()*/
					gis_id:this.txtGisId.getValue(),
					coords_info : this.txtCoordsInfo.getValue(),
					line_color : this.txtLineColor.getValue(),
					main_fill_color : this.txtMainFillColor.getValue(),
					groupCustInfoID:this.txtGroupCustInfoID.getValue(),
					type : this.txtGisType.getValue()
				},
				success:function(resp){	
					this.scope.hide();
					GridUnitInfo.List.instance.store.reload();
				}
			});
		},

		initGridGis:function(lineColor,mainFillColor,gridName,coordInfo,groupCustInfoID,gridId){
			this.txtLineColor.setValue(lineColor);
			this.txtMainFillColor.setValue(mainFillColor);
			this.txtSchoolName.setValue(gridName);
			this.txtCoordsInfo.setValue(coordInfo);
			this.txtGroupCustInfoID.setValue(groupCustInfoID);//社区当中所包含的集团客户ID集
			this.txtGridId.setValue(gridId);
		},
		viewGis:function(){
			var str = OMS.GIS_IP+'/gis_for_management.jsp?lineColor=' + this.txtLineColor.getValue() + 
			'&mainFillColor=' + this.txtMainFillColor.getValue() + 
			'&name='+this.txtUnitName.getValue()+
			'&type=1'+
			'&did='+this.txtUnitId.getValue()+ 
			'&coordx='+this.txtCoordsInfo.getValue()+ '&layerType=2';
			window.open(encodeURI(str), '_blank', 'toolbar=no,location=no,directories=no,scrollbars=no,menubar=no,width=1000,height=600,resizable=no,left=0,top=0, status=no');	
		}
		
});

//学校GIS 回调函数
 function initGridGis(lineColor,mainFillColor,gridName,coordInfo,groupCustInfoID,gridId){
	GridUnitInfo.Detail.instance.initGridGis(lineColor,mainFillColor,gridName,coordInfo,groupCustInfoID,gridId);
};